/**
 * Created by sicongdu on 18-4-16.
 */
const express = require('express');

var login = express.Router();

var configs = require("../mongo_non_student").configs
var teacher = require("../mongo_non_student").teachers_pass
var admin = require("../mongo_non_student").admins_pass

var getOpenId = require("./getOpenId").getOpenId

const uniform_password = "456789"


let queryIfSigned = (openid, res) => {
  //查询是否有注册
  var whereCondition = {
    openId: openid
  }

  configs.findOne(whereCondition, function (error, doc) {
    if (error) {
      res.send({
        error_no: 2,
        error_msg: "内部错误，请联系杜思聪～"
      })
    } else {
      if (doc == null) {
        res.send({
          error_no: 0,
          error_msg: "not signed up",
          openid: openid
        })

      } else {
        var getUserInfo = require("../getUserInfo").getUserInfo
        console.log(doc, doc.type, doc.key)
        getUserInfo(doc.type, doc.key, res)
      }
    }
  });
}

//查询是否注册并绑定
login.post("/query", (req, res, next) => {
  let { code } = req.body;

  getOpenId(code, queryIfSigned, res)
});

//查询ID是否被注册
login.post("/checkId", (req, res, next) => {
  var whereCondition = {
    key: req.body.key
  }

  configs.findOne(whereCondition, function (error, doc) {
    if (error) {
      res.send({
        error_no: 2,
        error_msg: "内部错误，请联系杜思聪～"
      })
    } else {
      if (doc == null) {
        res.send({
          error_no: 0,
          error_msg: "id not signed"
        })
      } else {
        res.send({
          error_no: 2,
          error_msg: "您的ID已经被注册～"
        })
      }
    }
  });
});


//login 老师，管理员
login.post("/check", (req, res, next) => {
  var { kind, id, password, code, userInfo } = req.body
  var bind_openID = (openid, res, doc) => {
    if (kind == "2") {
      var teachers = require("../mongo_non_student").teachers

      teachers.create({
        name: openid,
        teacherNo: openid,
        userInfo: openid,
        sex: openid,
        school: openid
      }, (error, doc) => {
        if (error) {
          res.send({
            error_no: 2,
            error_msg: "内部错误，请联系杜思聪～"
          })
        } else {
          var teachers_pass = require("../mongo_non_student").teachers_pass
          teachers_pass.create({
            teacherNo: openid,
            pass: uniform_password
          }, (error, doc) => {
            if (error) {
              res.send({
                error_no: 2,
                error_msg: "内部错误，请联系杜思聪～"
              })
            } else {
              var configs = require("../mongo_non_student").configs
              configs.create({
                type: "2",
                key: openid,
                openId: openid
              }, (error, doc) => {
                if (error) {
                  res.send({
                    error_no: 2,
                    error_msg: "内部错误，请联系杜思聪～"
                  })
                } else {
                  res.send({
                    error_no: 1,
                    error_msg: "ok"
                  })
                }
              })
            }
          })
        }
      })
    } else {
      //绑定微信openID
      var conditions = { type: kind, key: id };
      var updates = { $set: { openId: openid } };
      configs.update(conditions, updates, function (error) {
        if (error) {
          res.send({
            error_no: 2,
            error_msg: error + ""
          })
        } else {
          // if (kind == "2") {

          //   // var teachers = require("../mongo_non_student").teachers

          //   // var conditions = {teacherNo: id};
          //   // var updates = {$set: {userInfo: userInfo}};//更新用户信息
          //   // teachers.update(conditions, updates, function (error) {
          //   //   if (error) {
          //   //     res.send({
          //   //       error_no: 2,
          //   //       error_msg: error + ""
          //   //     })
          //   //   } else {
          //   //     res.send({
          //   //       error_no: 1,
          //   //       error_msg: "ok",
          //   //       user: doc
          //   //     })
          //   //   }
          //   // });
          // } else {
          res.send({
            error_no: 1,
            error_msg: "ok",
            user: doc
          })
          // }

        }
      });
    }

  }


  if (kind == '2') {
    if (password != uniform_password) {
      res.send({
        error_no: 2,
        error_msg: "对不起，密码错误～"
      })
    } else {

      getOpenId(code, bind_openID, res)
    }
    // teacher.findOne({
    //   teacherNo: id,
    //   pass: password
    // }, (error, doc) => {
    //   if (error) {
    //     res.send({
    //       error_no: 2,
    //       error_msg: error + ""
    //     })
    //   } else {
    //     if (doc == null) {
    //       res.send({
    //         error_no: 2,
    //         error_msg: "对不起，账号或密码错误～"
    //       })
    //     } else {
    //       getOpenId(code, bind_openID, res, doc)
    //     }
    //   }
    // })
  } else if (kind == '3') {
    admin.findOne({
      adminNo: id,
      pass: password
    }, (error, doc) => {
      if (error) {
        res.send({
          error_no: 2,
          error_msg: error + ""
        })
      } else {
        if (doc == null) {
          res.send({
            error_no: 2,
            error_msg: "对不起，账号或密码错误～"
          })
        } else {
          getOpenId(code, bind_openID, res, doc)
        }
      }
    })
  }
});

//signup
login.post("/signup", (req, res, next) => {
  var { setUserInfo } = require("../setUserInfo")
  setUserInfo(req, res)
});

//查询已有老师
login.post("/list_admin_teacher", (req, res, next) => {
  var where = {
    type: { "$in": ["2"] }
  }

  var result = {
    key: ""
  }

  configs.find(where, result, (error, document_array) => {
    if (error) {
      res.send({
        error_no: 2,
        error_msg: error + ""
      })
    } else {
      res.send({
        error_no: 0,
        error_msg: "ok",
        document_array: document_array
      })
    }
  })
});

var schools = require("../mongo_non_student").schools
//查询已有学校
login.post("/list_school", (req, res, next) => {

  schools.find({}, (error, document_array) => {
    if (error) {
      res.send({
        error_no: 2,
        error_msg: error + ""
      })
    } else {
      var documents = document_array
      res.send({
        error_no: 0,
        error_msg: "ok",
        document_array: documents
        //   .map((sch_doc) => {
        //   sch_doc._id = sch_doc._id.toString()
        // })
      })
    }
  })
});


module.exports = login;
